package com.xayy.health.ds.service.impl;

import cn.hutool.core.date.DatePattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xayy.health.mgt.model.dto.DataFeatureDto;
import com.xtck.ha.common.core.domain.R;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xtck.ha.common.core.config.DsServerConfig;
import com.xtck.ha.common.core.utils.DomainUtils;
import com.xayy.health.ds.model.DsTelemetryData;
import com.xayy.health.ds.service.DsTelemetryDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: DsTelemetryDataService实现类
 * @Auther:liuyuan
 * @Date:2025-07-08
 */
@Slf4j
@Service
public class DsTelemetryDataServiceImpl extends BaseServiceImpl<DsTelemetryData> implements DsTelemetryDataService {
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "TB_TELEMETRY_DATA";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }

    @Override
   public List<DsTelemetryData> getYcList(DataFeatureDto dataFeatureDto){
        SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
       String sql = "select\n" +
               " sate_code,\n" +
               " param_code,\n" +
               " value_type,\n" +
               " value,\n" +
               " time\n" +
               " from HEALTH.tb_telemetry_data where 1=1\n";
       if (null != dataFeatureDto.getSatelliteCode() && !dataFeatureDto.getSatelliteCode().equals(""))
           sql += " and sate_code = " +
                   "'" + dataFeatureDto.getSatelliteCode() + "'\n ";
       if (null != dataFeatureDto.getParamCode() && !dataFeatureDto.getParamCode().equals(""))
           sql += " and param_code = " +
                   "'"+dataFeatureDto.getParamCode()+"'"+
                   " \n";
       if (null != dataFeatureDto.getStartTime())
           sql += " and time   >=  " +
                   "'"+sdf.format(dataFeatureDto.getStartTime())+"'"+
                   " and time  <=  " +
                   "'"+sdf.format(dataFeatureDto.getEndTime())+"'"+
                   " \n" +
                   " order by time asc";
       return selectBySql(sql);
   }
    @Override
    public List<DsTelemetryData> getPhList(DataFeatureDto dataFeatureDto){
        SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
        String sql = "select * from HEALTH.tb_ph_data where 1=1\n";
        if (null != dataFeatureDto.getSatelliteCode() && !dataFeatureDto.getSatelliteCode().equals(""))
            sql += " and sate_code = \"" + dataFeatureDto.getSatelliteCode() + "\"\n ";
        if (null != dataFeatureDto.getParamCode() && !dataFeatureDto.getParamCode().equals(""))
            sql += " and param_code = " +
                    "'"+dataFeatureDto.getParamCode()+"'"+
                    " \n";
        if (null != dataFeatureDto.getTaskId() )
            sql += " and task_id = " +
                    dataFeatureDto.getTaskId()+
                    " \n";
        if (null != dataFeatureDto.getParentTaskId() )
            sql += " and parent_task_id = " +
                    dataFeatureDto.getParentTaskId()+
                    " \n";
        if (null != dataFeatureDto.getStartTime())
            sql += " and time   >=  " +
                    "'"+sdf.format(dataFeatureDto.getStartTime())+"'"+
                    " and time  <=  " +
                    "'"+sdf.format(dataFeatureDto.getEndTime())+"'"+
                    " \n" +
                    " order by time asc";
        return selectBySql(sql);
    }
}
